package com.bh.extend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.exception.ServiceException;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.extend.domain.JcCustomerUser;
import com.bh.extend.mapper.JcCustomerUserMapper;
import com.bh.extend.service.IJcCustomerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 客户用户Service业务层处理
 *
 * @author slx
 * @date 2023-04-03
 */
@Service
public class JcCustomerUserServiceImpl extends ServiceImpl<JcCustomerUserMapper, JcCustomerUser> implements IJcCustomerUserService {
    @Autowired
    private JcCustomerUserMapper jcCustomerUserMapper;

    @Override
    public JcCustomerUser selectJcCustomerUserByUserId(Long userId) {
        JcCustomerUser jcCustomerUser = new JcCustomerUser();
        jcCustomerUser.setUserId(userId);
        List<JcCustomerUser> customerUsers = jcCustomerUserMapper.selectJcCustomerUserList(jcCustomerUser);
        if (customerUsers == null || customerUsers.size() == 0) {
            return null;
        }
        return customerUsers.get(0);
    }

    @Override
    public JcCustomerUser selectJcCustomerUserByCustomerId(Long customerId) {
        JcCustomerUser jcCustomerUser = new JcCustomerUser();
        jcCustomerUser.setCustomerId(customerId);
        List<JcCustomerUser> customerUsers = jcCustomerUserMapper.selectJcCustomerUserList(jcCustomerUser);
        if (customerUsers == null || customerUsers.size() == 0) {
            return null;
        }
        return customerUsers.get(0);
    }

    /**
     * 查询客户用户列表
     *
     * @param jcCustomerUser 客户用户
     * @return 客户用户
     */
    @Override
    public List<JcCustomerUser> selectJcCustomerUserList(JcCustomerUser jcCustomerUser) {
        return jcCustomerUserMapper.selectJcCustomerUserList(jcCustomerUser);
    }

    /**
     * 新增客户用户
     *
     * @param jcCustomerUser 客户用户
     * @return 结果
     */
    @Override
    public int insertJcCustomerUser(JcCustomerUser jcCustomerUser) {
        // 检测，一个用户只能加入一次
        JcCustomerUser customerUser = this.selectJcCustomerUserByUserId(jcCustomerUser.getUserId());
        if (customerUser == null) {
            jcCustomerUser.setCreateBy(SecurityUtils.getUsername());
            jcCustomerUser.setCreateTime(DateUtils.getNowDate());
            return jcCustomerUserMapper.insert(jcCustomerUser);
        } else {
            throw new ServiceException("用户已经绑定，禁止重复进行绑定！");
        }
    }

    /**
     * 批量删除客户用户
     *
     * @param customerIds 需要删除的客户用户主键
     * @return 结果
     */
    @Override
    public int deleteJcCustomerUserByCustomerIds(String[] customerIds) {
        return jcCustomerUserMapper.deleteBatchIds(Arrays.asList(customerIds));
    }

    /**
     * 删除客户用户信息
     *
     * @param customerId 客户用户主键
     * @return 结果
     */
    @Override
    public int deleteJcCustomerUserByCustomerId(Long customerId) {
        return jcCustomerUserMapper.deleteById(customerId);
    }
}
